package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.List;

import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.ShopperEntity;
import com.yksj.healthtalk.ui.R;

public class FriendBaseAdapter extends BaseAdapter {

	private Context mcontext;
	private LayoutInflater inflater;
	private View clickView;
	private List<ShopperEntity> entitys=new ArrayList<ShopperEntity>();
	private ImageLoader loader; 
	onClickFriendAttentionListener attentionListener;
	onClickFriendHeadListener clickFriendHeadListener;
	public FriendBaseAdapter(Context mcontext) {
		super();
		this.mcontext = mcontext;
		inflater = LayoutInflater.from(mcontext);
		loader = ImageLoader.getInstance();
	}

	@Override
	public int getCount() {
		return entitys.size();
	}

	public void onBoundData(List<ShopperEntity> entity){
		entitys.addAll(entity);
		notifyDataSetChanged();
	}
	@Override
	public Object getItem(int position) {
		return entitys.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getClickView(){
		return clickView;
	}
	
	public void setClickView(View v){
		this.clickView = v;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		final ShopperEntity entity=(ShopperEntity) getItem(position);
		
		if(convertView==null){
			holder=new ViewHolder();
			convertView=inflater.inflate(R.layout.base_friend_item, null);
			holder.head_image=(ImageView) convertView.findViewById(R.id.head_image);
			holder.head_sex=(ImageView) convertView.findViewById(R.id.head_sex);
			holder.v=(ImageView) convertView.findViewById(R.id.v);
			holder.is_friend=(ImageView) convertView.findViewById(R.id.is_friend);
			holder.note=(TextView) convertView.findViewById(R.id.note);
			holder.name=(TextView) convertView.findViewById(R.id.name);
			convertView.setTag(holder);
		}
		holder=(ViewHolder) convertView.getTag();
		loader.displayImage(entity.getCLIENT_ICON_BACKGROUND(), holder.head_image);
		holder.note.setText(entity.getPERSONAL_NARRATE());
		//是否关注
		if("Y".equals(entity.getREL_TYPE())){//已关注
			holder.is_friend.setBackgroundResource(R.drawable.attention);
		}else{//未关注
			holder.is_friend.setBackgroundResource(R.drawable.not_attention);
		}
		
		holder.name.setText(entity.getCUSTOMER_NICKNAME());
		//性别
		if(entity.getCUSTOMER_SEX().equalsIgnoreCase("W")){
			holder.head_sex.setBackgroundResource(R.drawable.women);
		}else if(entity.getCUSTOMER_SEX().equalsIgnoreCase("M")){
			holder.head_sex.setBackgroundResource(R.drawable.man);
		}else{
			holder.head_sex.setBackgroundResource(R.drawable.sex_unknown);
		}
		//是否是医生
		if("Customer".equals(entity.getHEAD_PORTRAIT_TYPE())){
			holder.v.setVisibility(View.GONE);	
		}else{
			holder.v.setVisibility(View.VISIBLE);	
		}
		
		//点击头像进入个人资料
		holder.head_image.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (clickFriendHeadListener != null) {
					clickFriendHeadListener.onClickFriendHead(entity , position);
				}
			}
		});
		
		//点击取消和添加关注安妮
		holder.is_friend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (attentionListener != null) {
					attentionListener.onClickFriendAttention(entity.getREL_TYPE() ,entity , position );
				}
					setClickView(v);
			}
		});
		return convertView;
	}
	
	private class ViewHolder{
		private ImageView head_image;
		private ImageView head_sex;
		private ImageView v;
		private TextView note;
		private ImageView is_friend;
		private TextView name;
	}
	
	public interface onClickFriendHeadListener{
		void onClickFriendHead(ShopperEntity entity , int positon);
	}
	
	public void setonClickFriendHeadListener(onClickFriendHeadListener headListener){
		this.clickFriendHeadListener = headListener;
	};
	
	public interface onClickFriendAttentionListener{
		void onClickFriendAttention(String attentioned,ShopperEntity entity , int positon);
	}
	
	public void setonClickFriendAttentionListener(onClickFriendAttentionListener attentionListener){
		this.attentionListener = attentionListener;
	};
}
